<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="620px" top="10vh" @close='close'>
    <div class="dialogContentBoxMaxHeight80">
      <el-form ref="form" :model="record" label-width="170px" :rules="rules" label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('名称')" prop="name">
          <el-input v-emoji v-model="record.name" ref="recordName" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('本端地址')" prop="local">
          <el-input v-emoji v-model="record.local" ref="local" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('本地ISATAPipv6地址')" prop="ipv6_ip">
          <el-input v-emoji v-model="record.ipv6_ip" ref="ipv6Ip" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('最大路由通告时间(秒)')" prop="max_time">
          <el-input v-emoji v-model="record.max_time" ref="maxTime" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('最小路由通告时间(秒)')" prop="min_time">
          <el-input v-emoji v-model="record.min_time" ref="minTime" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('提示')">
          <span>{{$t('ISATAP地址格式是由链路层全局的IPv4地址和IPv6前缀组成，前缀00:0200:5efe:...用于全局独一无二的地址，前缀00::0000:5efe:...,用于私有地址,。例如全局IPv4地址192.0.2.143,接口IPv6地址为00::200:5efe:c000:28f(192.0.2.143).例如全局IPv4地址192.0.2.143,接口IPv6地址为00::200:5efe:c000:28f(192.0.2.143).')}}</span>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addisatap } from '@/api/network/tunnel/tunnel'
import { inputClass } from '@/utils/input-class.js'
export default {
  name: 'isatapDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (/^[1-9]+\d*$/i.test(value)) {
        callback(new Error(this.$t('名称不能为纯数字')))
      } else {
        callback()
      }
    }
    var validateIp = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    var validateMinTime = (rule, value, callback) => {
      var min = parseInt(this.record.max_time * 0.75)
      var info = this.$t('请输入3-') + min + this.$t('的数字')
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('请输入数字')))
      } else if (String(this.record.max_time).trim() === '') {
        callback(new Error(this.$t('请先填入最大路由通告时间')))
      } else if ((Number(this.record.max_time) < 4) || (Number(this.record.max_time) > 1800) || isNaN(Number(this.record.max_time))) {
        callback(new Error(this.$t('最大路由通告时间错误,无法判断最小路由通告时间')))
      } else if (Number(value) < 3 || Number(value) > min) {
        callback(new Error(info))
      } else {
        callback()
      }
    }
    var validateMaxTime = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('请输入数字')))
      } else if (Number(value) < 4 || Number(value) > 1800) {
        callback(new Error(this.$t('请输入4-1800的数字')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        name: '',
        local: '',
        ipv6_ip: '',
        max_time: '',
        min_time: ''
      },
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }],
        local: [{ required: false, validator: validateIp, trigger: 'blur' }],
        ipv6_ip: [{ required: false, validator: validateIp, trigger: 'blur' }],
        max_time: [{ required: false, validator: validateMaxTime, trigger: 'blur' }],
        min_time: [{ required: false, validator: validateMinTime, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init () {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['recordName', 'local', 'ipv6Ip', 'maxTime', 'minTime']
        inputClass(this.$refs, data)
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          addisatap(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('添加成功'),
                type: 'success'
              })
              this.close()
              this.$parent.listData()
            }
          })
        }
      })
    },
    close () {
      this.record.name = ''
      this.record.local = ''
      this.record.ipv6_ip = ''
      this.record.max_time = ''
      this.record.min_time = ''
      this.dialogVisible = false
      this.$refs.form.resetFields()
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    }
  }
}
</script>
